package com.mccken.practice.temp;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-12-31 22:57
 **/
public class MyStack {

	Queue<Integer> queue1 = new LinkedList<>();
	Queue<Integer> queue2 = new LinkedList<>();
	
	public MyStack() {

	}

	public void push(int x) {
		if (queue1.isEmpty()) {
			queue2.offer(x);
		} else {
			queue1.offer(x);	
		}
	}

	public int pop() {
		if (empty()) {
			return -1;
		}
		if (queue2.isEmpty()) {
			while (!queue1.isEmpty() && queue1.size() > 1) {
				queue2.offer(queue1.poll());
			}
			return queue1.poll();	
		} else {
			while (!queue2.isEmpty() && queue2.size() > 1) {
				queue1.offer(queue2.poll());
			}
			return queue2.poll();
		}
	}

	public int top() {
		if (empty()) {
			return -1;
		}
		if (queue2.isEmpty()) {
			while (!queue1.isEmpty() && queue1.size() > 1) {
				queue2.offer(queue1.poll());
			}
			Integer peek = queue1.peek();
			queue2.offer(queue1.poll());	
			return peek;
		} else {
			while (!queue2.isEmpty() && queue2.size() > 1) {
				queue1.offer(queue2.poll());
			}
			Integer peek = queue2.peek();
			queue1.offer(queue2.poll());
			return peek;
		}

	}

	public boolean empty() {
		return queue1.isEmpty() && queue2.isEmpty();
	}

	public static void main(String[] args) {
		MyStack myStack = new MyStack();
		myStack.push(1);
		myStack.push(2);
		System.out.println(myStack.top()); // 返回 2
		myStack.push(3);
		System.out.println(myStack.top()); // 返回 3
		System.out.println(myStack.empty()); // 返回 False
	}
}
